package me.zhengjie.modules.detailed.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.detailed.domain.Detailed;
import me.zhengjie.modules.detailed.service.DetailedService;
import me.zhengjie.modules.detailed.service.dto.DetailedDto;
import me.zhengjie.modules.detailed.service.dto.DetailedQueryParam;
import me.zhengjie.modules.detailed.service.mapper.DetailedMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author lida
* @date 2022-04-25
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DetailedService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DetailedServiceImpl extends CommonServiceImpl<DetailedMapper, Detailed> implements DetailedService {

    // private final RedisUtils redisUtils;
    private final DetailedMapper detailedMapper;

    @Override
    public PageInfo<DetailedDto> queryAll(DetailedQueryParam query, Pageable pageable) {
        IPage<Detailed> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<Detailed> page = detailedMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DetailedDto.class);
    }

    @Override
    public List<DetailedDto> queryAll(DetailedQueryParam query){

        return ConvertUtil.convertList(detailedMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DetailedDto.class);
    }

    @Override
    public Detailed getById(String id) {
        return detailedMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DetailedDto findById(String id) {
        return ConvertUtil.convert(getById(id), DetailedDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DetailedDto resources) {
        Detailed entity = ConvertUtil.convert(resources, Detailed.class);
        return detailedMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DetailedDto resources){
        Detailed entity = ConvertUtil.convert(resources, Detailed.class);
        int ret = detailedMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return detailedMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(String id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<String> ids) {
        for (String id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<DetailedDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DetailedDto detailed : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("财务类型", detailed.getType());
              map.put("分类id", detailed.getSortId());
              map.put("名称", detailed.getTitle());
              map.put("金额", detailed.getNum());
              map.put("是否删除(normal,draft)", detailed.getIsDeleted());
              map.put("创建时间", detailed.getGmtCreate());
              map.put("更新时间", detailed.getGmtModified());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
